package jp.sinya;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 获取客户端的请求信息
 */
public class DemoServlet8 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        resp.setHeader("Content-Type", "text/html; charset=utf-8");

        String reqAddress = req.getRemoteAddr();
        String reqMethod = req.getMethod();

        //User-Agent 是封装了客户端浏览器的基本信息的Header
        String userAgent = req.getHeader("User-Agent");

        String scheme = req.getScheme();
        String serverName = req.getServerName();
        int serverPort = req.getServerPort();
        String contextPath = req.getContextPath();//   /项目名
        String servletPath = req.getServletPath();//   /Servlet路径
        String query = req.getQueryString();//   /Servlet路径
        String requestURI = req.getRequestURI();//
        StringBuffer requestURL = req.getRequestURL();//


        PrintWriter writer = resp.getWriter();
        writer.write("<p>请求IP: " + reqAddress + "</p><br/>");
        writer.write("<p>请求方法: " + reqMethod + "</p><br/>");
        writer.write("<p>User-Agent的请求头: " + userAgent + "</p><br/>");
        writer.write("<p>scheme: " + scheme + "</p><br/>");
        writer.write("<p>serverName: " + serverName + "</p><br/>");
        writer.write("<p>serverPort: " + serverPort + "</p><br/>");
        writer.write("<p>contextPath: " + contextPath + "</p><br/>");
        writer.write("<p>servletPath: " + servletPath + "</p><br/>");
        writer.write("<p>query: " + query + "</p><br/>");
        writer.write("<p>requestURI: " + requestURI + "</p><br/>");
        writer.write("<p>requestURL: " + requestURL.toString() + "</p><br/>");
    }
}
